package com.haibei.view.web;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Point;
import android.graphics.Rect;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Window;

import com.common.uitl.StringUtils;
import com.haibei.App;
import com.haibei.view.web.Constant.DeviceConstant;

/**
 * 
 * 设备信息工具类 (提供获取设备屏幕、状态栏、IMEI号等与设备有关的相关信息)
 * 
 */
public final class DeviceInfoUtils {
	
	private static final String TAG =App.tag;
	/**
	 * 获取手机的电话号码
	 * 注意：不是所有的手机都可以得到
	 * @param context
	 * @return
	 */
	public static String getPhoneNumber(Context context){
		TelephonyManager telephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		if(telephonyMgr == null) {
			return "";
		}
		
		String number = telephonyMgr.getLine1Number();
		return number;
	}
	/**
	 * 取得屏幕宽度像素
	 * 
	 * @param activity
	 *            设备上下文
	 * @return 屏幕的宽度
	 */
	public static int getScreenWidth(Activity activity) {
		DisplayMetrics dm = getDisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm); // 得到屏幕大小
		return dm.widthPixels;
	}

	/**
	 * 取得屏幕高度像素
	 * 
	 * @param activity
	 *            设备上下文
	 * @return 屏幕的高度
	 */
	public static int getScreenHeight(Activity activity) {
		DisplayMetrics dm = getDisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm); // 得到屏幕大小
		return dm.heightPixels;
	}

	/**
	 * 取得状态栏高度
	 * 
	 * @param activity
	 *            设备上下文
	 * @return 状态栏高度
	 */
	public static int getStatusBarHeight(Activity activity) {
		Rect rectgle = new Rect();
		Window window = activity.getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		return rectgle.top;
	}

	/**
	 * 获取屏幕分辨率
	 * 
	 * @param context
	 * @return 获取到的屏幕分辨率
	 */
	public static String getDisplay(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		String resolution = screenWidth + "_" + screenHeight;
		Log.d(TAG, resolution);
		return resolution;
	}

	/**
	 * 获得屏幕的尺寸
	 * 
	 * @param context
	 * @return 获得屏幕的尺寸
	 */
	public static Point getDisplayMetrics(Context context) {
		Point dp = null;
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		int screenWidth = dm.widthPixels;
		int screenHeight = dm.heightPixels;
		dp = new Point(screenWidth, screenHeight);
		return dp;
	}

	/**
	 * 获取imei,如果获取不到，则生成一个15位号码
	 * 
	 * @return 获取imei,如果获取不到，则生成一个15位号码
	 */
	public static String getImei(final Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String imei;

		try {
			imei = tm.getDeviceId();// imei
		} catch (Exception e) {
			Log.e(TAG, "imei obtained exception", e);
			imei = null;
		}

		if (StringUtils.isEmpty(imei) || "0".equals(imei)) {
			// 如果imei号为空或0，取mac地址作为imei号传递
			WifiManager wifi = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			imei = info.getMacAddress();

			// 如果mac地址为空或0，则通过uuid生成的imei号来传递
			if (StringUtils.isEmpty(imei) || "0".equals(imei)) {

				imei = SharePersistentUtils.getPerference(context,DeviceConstant.IMEI);
				Log.d(TAG, "imei = " + imei);

				if (StringUtils.isEmpty(imei)) {
					imei = UUIDUtils.getUUID(15);
					if (StringUtils.isEmpty(imei)) {
						return "0";
					}
					Log.d(TAG, "imei new = " + imei);
					SharePersistentUtils.savePerference(context,DeviceConstant.IMEI, imei);
				}
			}
		}
		return imei;
	}

	/**
	 * 取得版本信息
	 * 
	 * @param context
	 * @return 版本Name
	 */
	public static String getVersionString(Context context) {
		String sPackageName = context.getPackageName();
		try {
			PackageManager pm = context.getPackageManager();
			PackageInfo pi = pm.getPackageInfo(sPackageName, 0);
			return String.valueOf(pi.versionName);
		} catch (NameNotFoundException e) {
			Log.d(TAG, "Could not retrieve package info", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获取屏幕的DisplayMetrics对象
	 * 
	 * @return DisplayMetrics对象
	 */
	public static DisplayMetrics getDisplayMetrics() {
		DisplayMetrics mDm;

		mDm = new DisplayMetrics();

		return mDm;
	}

	/**
	 * 获取wifi网络下的的MAC地址
	 * 
	 * @return MAC地址
	 */
	public static String getMacAddress(Context context) {
		WifiManager wifiManager = (WifiManager) context
				.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifiManager.getConnectionInfo();
		String macAddress = info.getMacAddress();
		if (TextUtils.isEmpty(macAddress)) {
			return "";
		} else {
			return macAddress;
		}
	}

	/**
	 * 从dip转化为px
	 * 
	 * @param context
	 * @param dipNum
	 *            要转换的dip值
	 * @return 转化后的像素值
	 */
	public static int fromDipToPx(Context context, int dipNum) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return dipNum * dm.densityDpi / 160;
	}

	/**
	 * 从dip转化为px
	 * 
	 * @param context
	 * @param dipNum
	 *            要转换的dip值
	 * @return 转化后的像素值
	 */
	public static float fromDipToPx(Context context, float dipNum) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return dipNum * dm.densityDpi / 160;
	}

	/**
	 * 取得density比例
	 * 
	 * @param context
	 * @return 当前设备Density的比例
	 */
	public static float getDensityScale(Context context) {
		DisplayMetrics dm = context.getResources().getDisplayMetrics();
		return dm.densityDpi * 1.0f / 160;
	}

	/**
	 * 获取设备的类型
	 * 
	 * @param context
	 * @return 设备的类型phoneType，如"GSM"
	 */
	public static final String getPhoneType(Context context) {
		String phoneType = null;

		try {
			android.telephony.TelephonyManager tel = (android.telephony.TelephonyManager) context
					.getSystemService(android.content.Context.TELEPHONY_SERVICE);

			int type = tel.getPhoneType();
			switch (type) {
			case android.telephony.TelephonyManager.PHONE_TYPE_GSM: {
				phoneType = "GSM";
			}
				break;
			case android.telephony.TelephonyManager.PHONE_TYPE_CDMA: {
				phoneType = "CDMA";
			}
				break;
			case android.telephony.TelephonyManager.PHONE_TYPE_NONE: {
				phoneType = "NONE";
			}
				break;
			default: {
				phoneType = String.valueOf(tel.getPhoneType());
			}
				break;
			}

		} catch (Exception e) {
			phoneType = "error";
		}

		return phoneType;
	}

	/**
	 * 获取sim卡运营商的类型
	 * 
	 * @param context
	 * @return 获取sim卡运营商的类型"ct"
	 */
	public static final String getSimOperatorType(Context context) {
		String simOperatorType = null;

		try {
			android.telephony.TelephonyManager tel = (android.telephony.TelephonyManager) context
					.getSystemService(android.content.Context.TELEPHONY_SERVICE);

			simOperatorType = tel.getSimOperator();
			if ("46000".equals(simOperatorType)) {
				simOperatorType = "cm";
			} else if ("46001".equals(simOperatorType)) {
				simOperatorType = "cuni";
			} else if ("46002".equals(simOperatorType)) {
				simOperatorType = "cm2";
			} else if ("46003".equals(simOperatorType)) {
				simOperatorType = "ct";
			}

		} catch (Exception e) {
			simOperatorType = "error";
		}

		return simOperatorType;
	}

}
